package com.neusoft.neuiotms.dcom.converter;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.utils.DateUtil;
import com.neusoft.neuiotms.dcom.constants.DCOMConstants;
import com.neusoft.neuiotms.dcom.dto.OperationSupportWeeklyDTO;
import com.neusoft.neuiotms.dcom.model.OperationSupportWeekly;
import com.neusoft.neuiotms.dcom.repository.OperationSupportWeeklyRepository;
import com.neusoft.neuiotms.dcom.repository.ProjectRepository;
import com.neusoft.neuiotms.dcom.vo.OperationSupportWeeklyImportVO;
import com.neusoft.neuiotms.dcom.vo.OperationSupportWeeklyVO;

@Component
public class OperationSupportWeeklyConverter
        extends AbstractConverter<OperationSupportWeekly, OperationSupportWeeklyDTO> {

    @Autowired
    private OperationSupportWeeklyRepository operationSupportWeeklyRepository;
    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public OperationSupportWeekly toModel(final OperationSupportWeeklyDTO dto) {

        OperationSupportWeekly model;
        if (dto.isNew()) {
            model = new OperationSupportWeekly();
        } else {
            model = this.operationSupportWeeklyRepository.getOne(dto.getId());
        }

        model.setCompanyName(dto.getCompanyName());
        model.setCompanyCode(dto.getCompanyCode());
        model.setUserCode(dto.getUserCode());
        model.setUserName(dto.getUserName());
        model.setServiceItem(dto.getServiceItem());
        model.setActualAttendanceNumber(dto.getActualAttendanceNumber());
        model.setWeekWorkInfo(dto.getWeekWorkInfo());
        model.setSummaryDesc(dto.getSummaryDesc());
        model.setProblemOrSolution(dto.getProblemOrSolution());
        model.setRemark(dto.getRemark());
        model.setNextWeekWorkPlan(dto.getNextWeekWorkPlan());
        model.setWeekDate(DateUtil.getStartTimeOfWeek(dto.getWeekDate()));
        model.setAttachment(dto.getAttachment());

        return model;
    }

    @Override
    public OperationSupportWeeklyDTO toDTO(final OperationSupportWeekly model, final boolean forListView) {

        final OperationSupportWeeklyDTO dto = new OperationSupportWeeklyDTO();

        dto.setId(model.getId());
        dto.setCompanyName(model.getCompanyName());
        dto.setCompanyCode(model.getCompanyCode());
        dto.setUserCode(model.getUserCode());
        dto.setUserName(model.getUserName());
        dto.setServiceItem(model.getServiceItem());
        dto.setActualAttendanceNumber(model.getActualAttendanceNumber());
        dto.setWeekWorkInfo(model.getWeekWorkInfo());
        dto.setSummaryDesc(model.getSummaryDesc());
        dto.setProblemOrSolution(model.getProblemOrSolution());
        dto.setRemark(model.getRemark());
        dto.setNextWeekWorkPlan(model.getNextWeekWorkPlan());
        dto.setWeekDate(DateUtil.getStartTimeOfWeek(model.getWeekDate()));
        dto.setAttachment(model.getAttachment());
        return dto;
    }

    public OperationSupportWeekly toModel(final OperationSupportWeeklyVO vo) {

        final OperationSupportWeekly model = new OperationSupportWeekly();

        model.setCompanyName(vo.getCompanyName());
        model.setCompanyCode(vo.getCompanyCode());
        model.setServiceItem(vo.getServiceItem());
        model.setActualAttendanceNumber(vo.getActualAttendanceNumber());
        model.setWeekWorkInfo(vo.getWeekWorkInfo());
        model.setSummaryDesc(vo.getSummaryDesc());
        model.setProblemOrSolution(vo.getProblemOrSolution());
        model.setRemark(vo.getRemark());
        model.setNextWeekWorkPlan(vo.getNextWeekWorkPlan());
        return model;
    }

    public OperationSupportWeeklyVO toVO(final OperationSupportWeekly model) {
        final OperationSupportWeeklyVO vo = new OperationSupportWeeklyVO();
        vo.setActualAttendanceNumber(model.getActualAttendanceNumber());
        vo.setCompanyName(model.getCompanyName());
        vo.setServiceItem(model.getServiceItem());
        vo.setWeekWorkInfo(model.getWeekWorkInfo());
        vo.setNextWeekWorkPlan(model.getNextWeekWorkPlan());
        vo.setProblemOrSolution(model.getProblemOrSolution());
        vo.setSummaryDesc(model.getSummaryDesc());
        vo.setRemark(model.getRemark());
        //        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        final Date weekDate = model.getWeekDate();
        final Calendar cal = Calendar.getInstance(TimeZone.getDefault());
        cal.setTime(weekDate);
        final int year = cal.get(Calendar.YEAR);
        vo.setYear(year);
        final int week = cal.get(Calendar.WEEK_OF_YEAR);
        vo.setWeek(this.firstDayIsInFristWeekOfYear(year) ? week : week - 1);
        return vo;
    }

    public OperationSupportWeekly toModel(final OperationSupportWeeklyImportVO vo) {
        final OperationSupportWeekly model = new OperationSupportWeekly();
        model.setActualAttendanceNumber(vo.getActualAttendanceNumber());
        model.setCompanyCode(vo.getCompany().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setCompanyName(vo.getCompany().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        model.setNextWeekWorkPlan(vo.getNextWeekWorkPlan());
        model.setProblemOrSolution(vo.getProblemOrSolution());
        model.setRemark(vo.getRemark());
        model.setServiceItem(this.projectRepository.findByCode(DCOMConstants.HN).getName());
        model.setSummaryDesc(vo.getSummaryDesc());
        model.setUserCode(vo.getUser().split(DCOMConstants.DICTIONARY_SPLITOR)[1]);
        model.setUserName(vo.getUser().split(DCOMConstants.DICTIONARY_SPLITOR)[0]);
        model.setWeekDate(DateUtil.getStartTimeOfWeek(vo.getWeekDate()));
        model.setWeekWorkInfo(vo.getWeekWorkInfo());
        return model;

    }

    public boolean firstDayIsInFristWeekOfYear(int year) {
        final Calendar cal = Calendar.getInstance();
        cal.set(year, 0, 1);
        final int day = (cal.get(Calendar.DAY_OF_WEEK) - 1) == 0 ? 7 : cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (day < 4) {
            return true;
        }
        return false;

    }
}
